package game.map.generator;

import game.graphics.Block;

import java.io.Serializable;
import java.util.Random;

import data.DataManager;

public class CaveGenerator implements Serializable
{
	public CaveGenerator(Block blocks[][])
	{
		this.blocks			= blocks;
		
		this.mapWidth		= blocks.length;
		this.mapHeight		= blocks[0].length;
	}
	
	public void pregenerate()
	{
		Random randomizer = new Random();
		
		int cavesCount = randomizer.nextInt(maxCavesCount - minCavesCount) + minCavesCount;
		
		for (int t = 0; t < cavesCount; t++)
		{
			int caveWidth	= randomizer.nextInt(maxCaveWidth);
			int caveHeight	= randomizer.nextInt(maxCaveHeight);
			
			int cavePosX	= randomizer.nextInt(mapWidth - caveWidth);
			int cavePosY	= randomizer.nextInt(mapHeight - minCaveY - caveHeight) + minCaveY;
			
			//System.out.println(cavePosX + " " + cavePosY);
			
			for (int i = cavePosX; i < cavePosX + caveWidth; i++)
			{
				for (int j = cavePosY; j < cavePosY + caveHeight; j++)
				{
					blocks[i][j].setBlockType(DataManager.BlockType.BLOCK_NONE);
				}
			}
		}	
	}
	
	private Block[][]	blocks;
	private int			mapWidth;
	private int 		mapHeight;
	
	private final int	maxCaveWidth	= 60;
	private final int	maxCaveHeight	= 20;
	private final int	minCaveY		= 600;
	private final int	maxCavesCount	= 200;
	private final int	minCavesCount	= 30;
}
